package com.runen.infinitemovement.track.recorduitl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.tool.CommonUtil;


/**
 * Create by LiaoDuanHong
 * Create on 2020/09/10 15:10
 */
public class BackStageManagementUtil {
    //获取手机类型
    public static String getMobileType() {
        String PHONE_NAME = null;
        if (Build.BRAND != null) {
            PHONE_NAME = Build.BRAND.toLowerCase();
        }
        return PHONE_NAME;
    }

    /***********************************厂商判断****************************************/
    //跳转至授权页面
    public static void jumpStartInterface(Context context) {
        try {
            Log.e("MANUFACTURER", "******************当前手机型号为：" + getMobileType());
            if (getMobileType().equals("xiaomi")) { // 红米Note4测试通过
                showActivity(context, "com.miui.powerkeeper",
                        "com.miui.powerkeeper.ui.HiddenAppsConfigActivity");
            } else if (getMobileType().equals("letv")) { // 乐视2测试通过
                showActivity(context, "com.letv.android.letvsafe",
                        "com.letv.android.letvsafe.AutobootManageActivity");
            } else if (getMobileType().equals("huawei") || getMobileType().equals("honor")) { // 华为测试通过
                try {
                    showActivity(context, "com.huawei.systemmanager",
                            "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
                } catch (Exception e) {
                    showActivity(context, "com.huawei.systemmanager",
                            "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
                }

            } else if (getMobileType().equals("vivo")) { // VIVO测试通过
                showActivity(context, "com.iqoo.secure");
            } else if (getMobileType().equals("meizu")) { //万恶的魅族
                showActivity(context, "com.meizu.safe");
            } else if (getMobileType().equals("oppo")) { // OPPO R8205测试通过
                try {
                    showActivity(context, "com.coloros.phonemanager");
                } catch (Exception e1) {
                    try {
                        showActivity(context, "com.oppo.safe");
                    } catch (Exception e2) {
                        try {
                            showActivity(context, "com.coloros.oppoguardelf");
                        } catch (Exception e3) {
                            showActivity(context, "com.coloros.safecenter");
                        }
                    }
                }
            } else if (getMobileType().equals("smartisan")) { // 锤子
                showActivity(context, "com.smartisanos.security");
            } else if (getMobileType().equals("samsung")) {
                try {
                    showActivity(context, "com.samsung.android.sm_cn");
                } catch (Exception e) {
                    showActivity(context, "com.samsung.android.sm");
                }
            }else {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {//抛出异常就直接打开设置页面
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    /***********************************厂商判断****************************************/

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void isIgnoringBatteryOptimizations(Context context) {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!pm.isIgnoringBatteryOptimizations(context.getPackageName())) {
                Intent intent = new Intent();
                String packageName = context.getPackageName();
                intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + packageName));
                context.startActivity(intent);
            }
        }
    }

    /**
     * 跳转到指定应用的首页
     */
    private static void showActivity(Context context, @NonNull String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        context.startActivity(intent);
    }

    /**
     * 跳转到指定应用的指定页面
     */
    private static void showActivity(Context context, @NonNull String packageName, @NonNull String activityDir) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, activityDir));
        intent.putExtra("package_name", context.getPackageName());
        intent.putExtra("package_label", CommonUtil.getString(R.string.app_name));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }


}
